/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.minhphuong.controllers;

import com.minhphuong.bll.*;
import com.minhphuong.entities.*;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author home
 */
@Controller
public class GioHangController {

    @RequestMapping(value = "/GioHang/Them/{id}", method = RequestMethod.POST)
    public String GioHangThem(ModelMap map, HttpSession session, @PathVariable int id) {
        HelperBLL.LoadGiaoDien(map, session);

        if (session.getAttribute("nguoiDung") == null) {
            return "redirect:/DangNhap?error=2";
        }

        SanPham sanphamCheck = SanPhamBLL.getSanPhamByID(id);

        // sản phẩm tồn tại
        if (sanphamCheck != null) {
            // kiểm tra sp còn hàng không?
            if (sanphamCheck.getSoLuongTon() - sanphamCheck.getSoLuongDangMua() == 0) {
                return "redirect:/?error_cart_0=" + sanphamCheck.getTenSanPham();
            }
            // đã có trong session
            int session_id = 0;
            if (session.getAttribute("" + id) != null) {
                session_id = (Integer) session.getAttribute("" + id);
            }
            if (session_id != 0) {
                // sản phẩm đơn hàng < 3
                if (session_id < 3) {
                    // giả sử sản phẩm + 1, còn đủ trong kho
                    if ((session_id + 1) <= (sanphamCheck.getSoLuongTon() - sanphamCheck.getSoLuongDangMua())) {
                        session.setAttribute("" + id, session_id + 1);
                    } else {
                        return "redirect:/?error_cart_hethang=" + sanphamCheck.getTenSanPham() + "&inventory=" + (sanphamCheck.getSoLuongTon() - sanphamCheck.getSoLuongDangMua());
                    }
                } else {
                    // sản phẩm đơn hàng >= 3
                    return "redirect:/?error_cart_3=1";

                }
            } else {
                // chưa có trong session
                session.setAttribute("" + id, 1);
            }

        }
        return "redirect:/GioHang/";
    }

    @RequestMapping(value = "/GioHang/", method = RequestMethod.GET)
    public String GioHang(ModelMap map, HttpSession session) {
        HelperBLL.LoadGiaoDien(map, session);

        if (session.getAttribute("nguoiDung") == null) {
            return "redirect:/DangNhap?error=2";
        }

        List<SanPham> listSanPhamGioHang = SanPhamBLL.getAllSanPhamGioHang(session);
        map.addAttribute("listSanPhamGioHang", listSanPhamGioHang);

        return "giohang";
    }

    @RequestMapping(value = "/GioHang/Xoa/{id}", method = RequestMethod.GET)
    public String GioHangXoa(ModelMap map, HttpSession session, @PathVariable int id) {
        HelperBLL.LoadGiaoDien(map, session);

        if (session.getAttribute("nguoiDung") == null) {
            return "redirect:/DangNhap?error=2";
        }

        SanPham sanphamCheck = SanPhamBLL.getSanPhamByID(id);

        // sản phẩm tồn tại
        if (sanphamCheck != null) {
            SanPhamBLL.deleteSanPhamGioHangByID(session, id);
        } else {
            if (id == 0) {
                SanPhamBLL.deleteAllSanPhamGioHang(session);
            }
        }
        return "redirect:/GioHang/";

    }

    @RequestMapping(value = "/GioHang/CapNhat/", method = RequestMethod.POST)
    public String GioHangCapNhat(ModelMap map, HttpSession session, HttpServletRequest request) {
        HelperBLL.LoadGiaoDien(map, session);

        SanPhamBLL.updateGioHang(session, request);

        return "redirect:/GioHang/";

    }

    @RequestMapping(value = "/GioHang/HoaDon/", method = RequestMethod.POST)
    public String GioHangHoaDon(ModelMap map, HttpSession session, HttpServletRequest request) {
        HelperBLL.LoadGiaoDien(map, session);

        String order_name = request.getParameter("order_name");
        String order_phone = request.getParameter("order_phone");
        String order_email = request.getParameter("order_email");
        String order_address = request.getParameter("order_address");
        String order_note = request.getParameter("order_note");
        String phuongthucthanhtoan = request.getParameter("phuongthucthanhtoan");

        map.addAttribute("order_name", order_name);
        map.addAttribute("order_phone", order_phone);
        map.addAttribute("order_email", order_email);
        map.addAttribute("order_address", order_address);
        map.addAttribute("order_note", order_note);
        map.addAttribute("phuongthucthanhtoan", phuongthucthanhtoan);

        map.addAttribute("order_date", new Timestamp((new Date()).getTime()));
        map.addAttribute("order_code", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
        List<SanPham> listSanPhamGioHang = SanPhamBLL.getAllSanPhamGioHang(session);
        map.addAttribute("listSanPhamGioHang", listSanPhamGioHang);

        return "giohang_donhang";
    }

    @RequestMapping(value = "/GioHang/HoaDon/HoanTat", method = RequestMethod.POST)
    public String GioHangHoaDonHoanTat(ModelMap map, HttpSession session, HttpServletRequest request) {
        HelperBLL.LoadGiaoDien(map, session);

        SanPhamBLL.submitGioHangHoaDon(session, request, map);
        
        return "redirect:/?order=1";
    }
}
